package com.jdew.thkj.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;

public class VoucherCenterActivity extends BaseActivity implements AMapLocationListener {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.mobile)
    TextView mobile;
    @Bind(R.id.video)
    TextView video;
    @Bind(R.id.QB)
    TextView QB;
    @Bind(R.id.yidong)
    TextView yidong;
    @Bind(R.id.liantong)
    TextView liantong;
    @Bind(R.id.dianxin)
    TextView dianxin;
    @Bind(R.id.charge_phone)
    TextView chargePhone;
    @Bind(R.id.charge_g)
    TextView chargeG;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv3)
    TextView tv3;
    @Bind(R.id.tv4)
    TextView tv4;
    @Bind(R.id.tv5)
    TextView tv5;
    @Bind(R.id.tv6)
    TextView tv6;
    @Bind(R.id.tv7)
    TextView tv7;
    @Bind(R.id.tv8)
    TextView tv8;
    @Bind(R.id.province)
    TextView tvprovince;
    @Bind(R.id.mobile_view)
    LinearLayout mobileView;
    @Bind(R.id.g_view)
    LinearLayout gView;
    @Bind(R.id.tv9)
    TextView tv9;
    @Bind(R.id.tv10)
    TextView tv10;
    @Bind(R.id.tv11)
    TextView tv11;
    @Bind(R.id.tv12)
    TextView tv12;
    @Bind(R.id.tv13)
    TextView tv13;
    @Bind(R.id.tv14)
    TextView tv14;
    @Bind(R.id.tv15)
    TextView tv15;
    @Bind(R.id.tv16)
    TextView tv16;
    @Bind(R.id.mobilecharge_view)
    LinearLayout mobilechargeView;
    @Bind(R.id.aqy)
    TextView aqy;
    @Bind(R.id.td)
    TextView td;
    @Bind(R.id.tx)
    TextView tx;
    @Bind(R.id.sg)
    TextView sg;
    @Bind(R.id.mg)
    TextView mg;
    @Bind(R.id.month)
    TextView month;
    @Bind(R.id.season)
    TextView season;
    @Bind(R.id.half_year)
    TextView halfYear;
    @Bind(R.id.year)
    TextView year;
    @Bind(R.id.video_view)
    LinearLayout videoView;
    @Bind(R.id.areaview)
    LinearLayout areaview;
    @Bind(R.id.search)
    TextView search;
    @Bind(R.id.no)
    TextView no;
    private List<TextView> list1 = new ArrayList<>();
    private List<TextView> list2 = new ArrayList<>();
    private List<TextView> list3 = new ArrayList<>();
    private List<TextView> list4 = new ArrayList<>();
    private List<TextView> list5 = new ArrayList<>();
    private String provincecity;
    AMapLocationClient mlocationClient;
    AMapLocationClientOption mLocationOption;
    private int type = 0;
    private String keywords;


    @Override
    public int bindLayout() {
        return R.layout.activity_vouchercenter;
    }

    @Override
    public void initView(View view) {
        title.setText("充值中心");
        back.setVisibility(View.VISIBLE);
        mobile.setSelected(true);
        yidong.setSelected(true);
        tv5.setSelected(true);
        aqy.setSelected(true);
        month.setSelected(true);
        //判断是否为android6.0系统版本，如果是，需要动态添加权限
        if (Build.VERSION.SDK_INT >= 23) {
            showContacts();
        } else {
            new GaoDeDingWei().aMapLocationByNet();
        }
        month.setTextColor(getResources().getColor(R.color.theme_color));
        chargePhone.setTextColor(getResources().getColor(R.color.theme_color));
        yidong.setTextColor(getResources().getColor(R.color.theme_color));
        tv5.setTextColor(getResources().getColor(R.color.theme_color));
        list1.add(yidong);
        list1.add(liantong);
        list1.add(dianxin);
        list2.add(tv1);
        list2.add(tv2);
        list2.add(tv3);
        list2.add(tv4);
        list2.add(tv5);
        list2.add(tv6);
        list2.add(tv7);
        list2.add(tv8);
        list3.add(tv9);
        list3.add(tv10);
        list3.add(tv11);
        list3.add(tv12);
        list3.add(tv13);
        list3.add(tv14);
        list3.add(tv15);
        list3.add(tv16);
        list4.add(aqy);
        list4.add(td);
        list4.add(tx);
        list4.add(sg);
        list4.add(mg);
        list5.add(month);
        list5.add(season);
        list5.add(halfYear);
        list5.add(year);
        back.setOnClickListener(this);
        mobile.setOnClickListener(this);
        video.setOnClickListener(this);
        QB.setOnClickListener(this);
        yidong.setOnClickListener(this);
        liantong.setOnClickListener(this);
        dianxin.setOnClickListener(this);
        chargePhone.setOnClickListener(this);
        chargeG.setOnClickListener(this);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
        tv6.setOnClickListener(this);
        tv7.setOnClickListener(this);
        tv8.setOnClickListener(this);
        tv9.setOnClickListener(this);
        tv10.setOnClickListener(this);
        tv11.setOnClickListener(this);
        tv12.setOnClickListener(this);
        tv13.setOnClickListener(this);
        tv14.setOnClickListener(this);
        tv15.setOnClickListener(this);
        tv16.setOnClickListener(this);
        aqy.setOnClickListener(this);
        td.setOnClickListener(this);
        tx.setOnClickListener(this);
        sg.setOnClickListener(this);
        mg.setOnClickListener(this);
        month.setOnClickListener(this);
        season.setOnClickListener(this);
        halfYear.setOnClickListener(this);
        year.setOnClickListener(this);
        areaview.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.areaview: {
                initCityPicker();
            }
            break;
            case R.id.mobile: {
                type = 0;
                mobile.setSelected(true);
                video.setSelected(false);
                mobilechargeView.setVisibility(View.VISIBLE);
                videoView.setVisibility(View.GONE);
            }
            break;
            case R.id.video: {
                type = 1;
                mobile.setSelected(false);
                video.setSelected(true);
                mobilechargeView.setVisibility(View.GONE);
                videoView.setVisibility(View.VISIBLE);
            }
            break;
            case R.id.yidong: {
                for (TextView tv : list1) {
                    tv.setSelected(false);
                }
                yidong.setSelected(true);
                yidong.setTextColor(getResources().getColor(R.color.theme_color));
                liantong.setTextColor(getResources().getColor(R.color.top_title));
                dianxin.setTextColor(getResources().getColor(R.color.top_title));
            }
            break;
            case R.id.liantong: {
                for (TextView tv : list1) {
                    tv.setSelected(false);
                }
                liantong.setSelected(true);
                liantong.setTextColor(getResources().getColor(R.color.theme_color));
                yidong.setTextColor(getResources().getColor(R.color.top_title));
                dianxin.setTextColor(getResources().getColor(R.color.top_title));
            }
            break;
            case R.id.dianxin: {
                for (TextView tv : list1) {
                    tv.setSelected(false);
                }
                dianxin.setSelected(true);
                dianxin.setTextColor(getResources().getColor(R.color.theme_color));
                yidong.setTextColor(getResources().getColor(R.color.top_title));
                liantong.setTextColor(getResources().getColor(R.color.top_title));
            }
            break;
            case R.id.charge_phone: {
                type = 0;
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv5.setSelected(true);
                tv5.setTextColor(getResources().getColor(R.color.theme_color));
                chargePhone.setTextColor(getResources().getColor(R.color.theme_color));
                chargeG.setTextColor(getResources().getColor(R.color.top_title));
                mobileView.setVisibility(View.VISIBLE);
                gView.setVisibility(View.GONE);

            }
            break;
            case R.id.charge_g: {
                type = 2;
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv13.setSelected(true);
                tv5.setTextColor(getResources().getColor(R.color.theme_color));
                tv13.setTextColor(getResources().getColor(R.color.theme_color));
                chargeG.setTextColor(getResources().getColor(R.color.theme_color));
                chargePhone.setTextColor(getResources().getColor(R.color.top_title));
                mobileView.setVisibility(View.GONE);
                gView.setVisibility(View.VISIBLE);

            }
            break;
            case R.id.tv1: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv1.setSelected(true);
                tv1.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv2: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv2.setSelected(true);
                tv2.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv3: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv3.setSelected(true);
                tv3.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv4: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv4.setSelected(true);
                tv4.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv5: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv5.setSelected(true);
                tv5.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv6: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv6.setSelected(true);
                tv6.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv7: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv7.setSelected(true);
                tv7.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv8: {
                for (TextView tv : list2) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv8.setSelected(true);
                tv8.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv9: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv9.setSelected(true);
                tv9.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv10: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv10.setSelected(true);
                tv10.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv11: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv11.setSelected(true);
                tv11.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv12: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv12.setSelected(true);
                tv12.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv13: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv13.setSelected(true);
                tv13.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv14: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv14.setSelected(true);
                tv14.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv15: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv15.setSelected(true);
                tv15.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.tv16: {
                for (TextView tv : list3) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                tv16.setSelected(true);
                tv16.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.aqy: {
                no.setVisibility(View.GONE);
                halfYear.setVisibility(View.VISIBLE);
                for (TextView tv : list4) {
                    tv.setSelected(false);
                }
                aqy.setSelected(true);
            }
            break;
            case R.id.td: {
                no.setVisibility(View.GONE);
                halfYear.setVisibility(View.VISIBLE);
                for (TextView tv : list4) {
                    tv.setSelected(false);
                }
                td.setSelected(true);
            }
            break;
            case R.id.tx: {
                no.setVisibility(View.GONE);
                halfYear.setVisibility(View.VISIBLE);
                for (TextView tv : list4) {
                    tv.setSelected(false);
                }
                tx.setSelected(true);
            }
            break;
            case R.id.sg: {
                for (TextView tv : list5) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                halfYear.setVisibility(View.GONE);
                month.setSelected(true);
                no.setVisibility(View.INVISIBLE);
                month.setTextColor(getResources().getColor(R.color.theme_color));
                for (TextView tv : list4) {
                    tv.setSelected(false);
                }
                sg.setSelected(true);
            }
            break;
            case R.id.mg: {
                no.setVisibility(View.GONE);
                halfYear.setVisibility(View.VISIBLE);
                for (TextView tv : list4) {
                    tv.setSelected(false);
                }
                mg.setSelected(true);
            }
            break;
            case R.id.month: {
                for (TextView tv : list5) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                month.setSelected(true);
                month.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.season: {
                for (TextView tv : list5) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                season.setSelected(true);
                season.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.half_year: {
                for (TextView tv : list5) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                halfYear.setSelected(true);
                halfYear.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.year: {
                for (TextView tv : list5) {
                    tv.setSelected(false);
                    tv.setTextColor(getResources().getColor(R.color.top_title));
                }
                year.setSelected(true);
                year.setTextColor(getResources().getColor(R.color.theme_color));
            }
            break;
            case R.id.QB: {
                Intent intent = new Intent(this, ChargeCenterActivity.class);
                intent.putExtra("result", "腾讯QB");
                startActivity(intent);
            }
            break;
            case R.id.search: {
                if (type == 0) {
                    for (TextView tv : list1) {
                        if (tv.isSelected()) {
                            if (!TextUtils.isEmpty(tvprovince.getText())) {
                                if (tvprovince.getText().toString().length() >= 3) {
                                    keywords = tvprovince.getText().toString().substring(0, 2).equals("黑龙") ? "黑龙江" : tvprovince.getText().toString().substring(0, 2) + tv.getText().toString();
                                }else{
                                    return;
                                }
                            }
                        }
                    }
                    for (TextView tv : list2) {
                        if (tv.isSelected()) {
                            keywords += tv.getText().toString() + "充值卡";
                        }
                    }
                } else if (type == 1) {
                    for (int i = 0; i < list4.size(); i++) {
                        if (list4.get(i).isSelected()) {
                            if (i == 0) {
                                keywords = "爱奇艺会员";
                            } else if (i == 1) {
                                keywords = "优酷土豆会员";
                            } else if (i == 2) {
                                keywords = "腾讯视频会员";
                            } else if (i == 3) {
                                keywords = "搜狐视频会员";
                            } else if (i == 4) {
                                keywords = "芒果TV会员";
                            }
                        }
                    }
                    for (TextView tv : list5) {
                        if (tv.isSelected()) {
                            keywords += tv.getText().toString();
                        }
                    }
                } else {
                    for (TextView tv : list1) {
                        if (tv.isSelected()) {
                            if (!TextUtils.isEmpty(tvprovince.getText())) {
                                keywords = tvprovince.getText().toString().substring(0, 2).equals("黑龙") ? "黑龙江" : tvprovince.getText().toString().substring(0, 2) + tv.getText().toString();
                            }
                        }
                    }
                    for (TextView tv : list3) {
                        if (tv.isSelected()) {
                            keywords += tv.getText().toString() + "流量充值卡";
                        }
                    }
                }
                Intent intent = new Intent(this, ChargeCenterActivity.class);
                intent.putExtra("result", keywords);
                startActivity(intent);
            }
            break;
            default:
                break;
        }

    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                provincecity = amapLocation.getProvince();
                tvprovince.setText(provincecity);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(amapLocation.getTime());
                df.format(date);//定位时间
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }

    //6.0定位需要的权限申请
    public void showContacts() {
        if (ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_COARSE_LOCATION")
                != PackageManager.PERMISSION_GRANTED) {
            ToastUtils.getInstance(this).showMessage("定位权限未开启,请手动开启定位权限");
            // 申请一个（或多个）权限，并提供用于回调返回的获取码（用户定义）
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 100);
        } else {
            new GaoDeDingWei().aMapLocationByNet();
        }
    }


    //高德定位
    class GaoDeDingWei {
        public GaoDeDingWei() {

        }

        public void aMapLocationByNet() {
            //获取网络管理对象
            ConnectivityManager connmanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取当前活跃的网络
            NetworkInfo info = connmanager.getActiveNetworkInfo();
            if (info == null) {
                ToastUtils.getInstance(VoucherCenterActivity.this).showMessage("请打开网络");
            } else {
                //city_tv.setText("定位中，请稍等");
                aMapLocation();
            }
        }
    }

    public void aMapLocation() {
        mlocationClient = new AMapLocationClient(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mlocationClient.setLocationListener(this);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(3600 * 24);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mlocationClient.startLocation();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 100://刚才的识别码
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//用户同意权限,执行我们的操作
                    aMapLocation();
                } else {
                    //用户拒绝之后,当然我们也可以弹出一个窗口,直接跳转到系统设置页面
                }
                break;
            default:
                break;
        }
    }

    public void initCityPicker() {
        OptionsPickerView<String> mOptionsPickerView = new OptionsPickerView<>(this);
        final ArrayList<String> list = new ArrayList<>();
        list.add("北京市");
        list.add("天津市");
        list.add("上海市");
        list.add("重庆市");
        list.add("河北省");
        list.add("山西省");
        list.add("辽宁省");
        list.add("吉林省");
        list.add("黑龙江省");
        list.add("江苏省");
        list.add("浙江省");
        list.add("安徽省");
        list.add("福建省");
        list.add("江西省");
        list.add("山东省");
        list.add("河南省");
        list.add("湖北省");
        list.add("湖南省");
        list.add("广东省");
        list.add("甘肃省");
        list.add("四川省");
        list.add("贵州省");
        list.add("海南省");
        list.add("云南省");
        list.add("陕西省");
        list.add("广西壮族自治区");
        list.add("西藏自治区");
        list.add("宁夏回族自治区");
        list.add("新疆维吾尔自治区");
        list.add("内蒙古自治区");
        mOptionsPickerView.setPicker(list);
        mOptionsPickerView.setTitle("地区选择");
        // 设置选项单位
//        mOptionsPickerView.setLabels("性");
        mOptionsPickerView.setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int option1, int option2, int option3) {
                String provinence = list.get(option1);
                tvprovince.setText(provinence);
            }
        });
        mOptionsPickerView.show();
    }

}
